package com.youlemall.shop.realm;

import com.alibaba.dubbo.config.annotation.Reference;
import com.youlemall.pojo.Seller;
import com.youlemall.service.SellerService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

/**
 * 2 * @Author: kevin
 * 3 * @Date: 2019/7/18 21:45
 * 4
 */
public class SellerAuthorizingRealm extends AuthorizingRealm {
	@Reference
	private SellerService sellerService;

	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
		return null;
	}

	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
		try {
			String username = (String)authenticationToken.getPrincipal();
			Seller seller = sellerService.findOne(username);
			if(seller!=null && "1".equals(seller.getStatus())){
				SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(username, seller.getPassword(),
						ByteSource.Util.bytes(seller.getSellerId()),this.getName());
				return simpleAuthenticationInfo;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
